package com.hxy.health.platform.controller;

import com.hxy.health.platform.common.Const;
import com.hxy.health.platform.common.ServerResponse;
import com.hxy.health.platform.pojo.Appointment;
import com.hxy.health.platform.pojo.User;
import com.hxy.health.platform.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    /**
     * 跳转到预定界面
     */
    @RequestMapping("page/appointment")
    public String appointmentPage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "loginOrRegister";
        }
        Appointment appointment = appointmentService.getByEmail(user.getEmail());
        modelMap.addAttribute("appointment", appointment);
        modelMap.addAttribute("user", user);
        return "appointment";
    }

    /**
     * 预定到店
     */
    @RequestMapping("appointment/appointment")
    @ResponseBody
    public ServerResponse appointment(Appointment appointment) {
        int result = appointmentService.addAppointment(appointment);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("预定成功");
        }
        return ServerResponse.createBySuccessMessage("预定失败");
    }

    /**
     * 预定到店
     */
    @RequestMapping("appointment/appointmentEdit")
    @ResponseBody
    public ServerResponse appointmentEdit(Appointment appointment) {
        int result = appointmentService.editAppointment(appointment);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("预定成功");
        }
        return ServerResponse.createBySuccessMessage("预定失败");
    }

}
